Skip to content

Conversation

@osamakader
Copy link
Contributor

Replaces the deprecated strings.Title call in criuNsToKey() with a manual capitalization of the first character.
The behavior remains identical for ASCII namespace names while ensuring compatibility with modern Go versions.

Comment on lines 186 to 191
nsName := configs.NsName(t)
// Capitalize first letter manually to avoid deprecated strings.Title
if len(nsName) > 0 {
nsName = strings.ToUpper(nsName[:1]) + nsName[1:]
}
return "extRoot" + nsName + "NS"
Copy link
Contributor

@kolyshkin kolyshkin Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks kind of ugly, but I came up with this:

  func criuNsToKey(t configs.NamespaceType) string {
          // Construct "extRoot" + capitalize(nsName) + "NS" without allocations.
          // Result format: "extRootNetNS", "extRootPidNS", etc.
          nsName := configs.NsName(t)
          out := make([]byte, 0, 64)
          out = append(out, "extRoot"...)
          // Capitalize the first character (this assumes it's in the a-z range).
          out = append(out, nsName[0]-'a'+'A')
          out = append(out, nsName[1:]...)
          out = append(out, "NS"...)

          return string(out)
  }

TBH I wish we can just drop the capitalization, but we'll lose backward compatibility (restore from old version won't work).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't it panic if nsName is empty; accessing "nsName[0]", in out = append(out, nsName[0]-'a'+'A')?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it will. This is a simplified code which assumes that nsName is not empty and its first character is a lowercase ASCII. I think these assumptions are correct given the current codebase, but of course it's better be safe than sorry. So please add a check.

I also wrote a benchmark to see how fast is the code and how many allocations it does, and the above is the fastest I could do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While manually fiddling with bytes is probably more efficient, I would've preferred using strings.Builder to be honest.

Also, nsName[0]-'a'+'A' is kinda ugly -- I would use unicode.ToUpper (which does that internally for ASCII characters).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using strings.Builder for a 16 character string is an overkill.

Using unicode.ToUpper is fine I guess.

         // Capitalize the first character.
        out = append(out, byte(unicode.ToUpper(rune(nsName[0]))))

@osamakader osamakader force-pushed the fix-stringsTitle-criu-linux branch from 4a852dd to 3a0b5e8 Compare October 8, 2025 19:10
@osamakader osamakader requested a review from kolyshkin October 8, 2025 19:17
Copy link
Contributor

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also add me as a coauthor (Co-authored-by:).

@osamakader osamakader force-pushed the fix-stringsTitle-criu-linux branch from 3a0b5e8 to db302e2 Compare October 8, 2025 20:17
@osamakader
Copy link
Contributor Author

You can also add me as a coauthor (Co-authored-by:).

My pleasure :)

@osamakader osamakader force-pushed the fix-stringsTitle-criu-linux branch from db302e2 to 418e4e3 Compare October 8, 2025 21:01
@osamakader osamakader requested a review from kolyshkin October 8, 2025 21:15
Copy link
Contributor

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks

@kolyshkin kolyshkin requested review from lifubang and rata October 9, 2025 02:14
@lifubang
Copy link
Member

lifubang commented Oct 9, 2025

@osamakader Please update your commit message subject to: "criu: replace deprecated strings.Title", as it should be kept under 72 characters. Thanks.

@kolyshkin @osamakader Just to discuss—why not use methods from Go's standard packages or official libraries? For example:

  1. From "golang.org/x/text
cases.Title(language.English).String(someText)
  1. From "unicode"
unicode.ToUpper(rune)

While this manual capitalization approach is simple, I strongly recommend using standard library methods. That said, it's not a blocker for merging.

@kolyshkin
Copy link
Contributor

  1. From "golang.org/x/text

I'd rather not introduce another dependency for such trivial stuff.

@lifubang
Copy link
Member

I'd rather not introduce another dependency for such trivial stuff.

That makes sense.

What do you think about this approach: out = append(out, byte(unicode.ToUpper(rune(nsName[0]))))?
Further more, I think we can use strings.ToUpper(nsName[0]) directly.

@rata
Copy link
Member

rata commented Oct 10, 2025

@osamakader thanks! I agree with @lifubang and @cyphar here. Let's try to find a simpler way to do this. x/text is even suggested in the documentation for the deprecated function. Can you explore the options suggested by others and see what is simpler for this use case?

@osamakader osamakader force-pushed the fix-stringsTitle-criu-linux branch from 418e4e3 to f4c7571 Compare October 11, 2025 20:32
@lifubang
Copy link
Member

@osamakader Please update your commit message subject to: "criu: replace deprecated strings.Title", as it should be kept under 72 characters. Thanks.

@osamakader

strings.Title is deprecated since Go 1.18. Replace it with a simple
manual capitalization of the first character in criuNsToKey().

Signed-off-by: Osama Abdelkader <[email protected]>
Co-authored-by: Kir Kolyshkin <[email protected]>
@osamakader osamakader force-pushed the fix-stringsTitle-criu-linux branch from f4c7571 to 1adb070 Compare October 12, 2025 11:03
@osamakader osamakader changed the title criu: replace deprecated strings.Title with manual capitalization criu: replace deprecated strings.Title Oct 12, 2025
@lifubang lifubang merged commit fa1b693 into opencontainers:main Oct 13, 2025
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants